package cn.bluedot.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bluedot.dao.BackupAndRecoverDao;
import cn.bluedot.model.Backup;
import cn.bluedot.model.BackupInfo;
import cn.bluedot.service.BackupAndRecoverService;
import cn.bluedot.util.MySQLDatabaseBackupUtils;

@Service
public class BackupAndRecoverServiceImpl implements BackupAndRecoverService {

    @Autowired
    BackupAndRecoverDao backupAndRecoverDao;
    
    @Override
    public void simpleTableBackup(Backup backup) {
        //创建备份信息类
        BackupInfo info = new BackupInfo();
        info.setHost("localhost");
        // 数据库用户名及密码应该通过解析XML文件获取 此处采用硬编码
        info.setUsername("root");
        info.setPassword("root");
        info.setSavePath(backup.getSavePath());
        info.setFileName(backup.getBackupFilename());
        //数据库名应通过XML解析获取 此处采用硬编码
        info.setDatabaseName("intelligentpigbreedingsystem");
        info.setTableNames(backup.getDatabaseTable());
        //TODO 此处采用本地服务器的MySQL Bin 可以将mysqldump放到项目目录中 获取 mysqlBin
        info.setMysqlBin("C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin");
        //将单个表备份 
        MySQLDatabaseBackupUtils.backup(info.getHost(), info.getUsername(), info.getPassword(), info.getSavePath(),
                info.getFileName(), info.getDatabaseName(), backup.getDatabaseTable(), info.getMysqlBin());
                
        Date date = new Date();
        backup.setBackupTime(date);
        backup.setModifyTime(date);
        //将备份记录保存到数据库中
        backupAndRecoverDao.simpleTableBackup(backup);
    }

    @Override
    public void dataBaseRecover(String fromPath) {
        // 将单个表恢复 
        BackupAndRecoverServiceImpl.recover("intelligentpigbreedingsystem", "");
        // 记录恢复信息保存到数据库中
        
    }
    /**
     * 恢复整个数据库 
     * @param dataBaseName  数据库名
     * @param fromBackupFilePath   目标恢复文件路径
     */
    public static void recover(String dataBaseName, String fromBackupFilePath) {
        try {
            Runtime  rt =  Runtime.getRuntime();
            Process process  = rt.exec("C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysql.exe -hlocalhost -uroot -proot --default-character-set=utf8 "+dataBaseName);
            OutputStream out = process.getOutputStream();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(fromBackupFilePath), "utf-8"));
            
            String str = null;
            StringBuffer  buffer =  new StringBuffer();
            while((str=bufferReader.readLine())!=null) {
                buffer.append(str+"\r\n");
            }
            str  = buffer.toString();
            
            OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
            writer.write(str);
            writer.flush();
            
            out.close();
            bufferReader.close();
            writer.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
}
